package com.hospital.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hospital.mapper.HospusersMapper;
import com.hospital.mapper.LoginOutMapper;
import com.hospital.mapper.RoleMapper;
import com.hospital.mapper.UserRoleMapper;
import com.hospital.pojo.*;
import com.hospital.vo.ResultVO;
import com.hospital.vo.UserWithRole;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LoginOutService {
    @Resource
    private LoginOutMapper loginOutMapper;

    @Resource
    private HospusersMapper hospusersMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public UserHardCoded selectUserHardCodedByName(String userName, String password){
        UserHardCoded userHardCoded = loginOutMapper.selectUserByName(userName, password);
        return userHardCoded;
    }

    /**
     * 验证用户登录
     * @param userName
     * @param password
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Hospusers> selectHospUserByUsername(String userName, String password){
        HospusersExample example = new HospusersExample();
        HospusersExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        criteria.andPasswordEqualTo(password);
        List<Hospusers> hospusers = hospusersMapper.selectByExample(example);
        return hospusers;
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public UserHardCoded login(String userName, String password){
        System.out.println(userName);
        System.out.println(password);
        UserHardCoded userHardCoded = loginOutMapper.login(userName, password);
        return userHardCoded;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int modifyPwd(Hospusers hospusers) {
        int update = hospusersMapper.updateByPrimaryKeySelective(hospusers);
        return update;
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<UserWithRole> getUserInfoWithRole(@Nullable String username, Integer currentPageInput, Integer pageSize){
        PageHelper.startPage(currentPageInput, pageSize);
        PageHelper.orderBy("username asc");
//        System.out.println(username + " " + currentPageInput + " " + pageSize) ;
        List<UserWithRole> userWithRoles = hospusersMapper.selectByUsername(username);
        return new PageInfo<>(userWithRoles);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Hospusers getHospUserDetail(Integer userid){
        Hospusers hospusers = hospusersMapper.selectByPrimaryKey(userid);
        return hospusers;
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Role> getRoleList(){
        RoleExample roleExample = new RoleExample();
        List<Role> roles = roleMapper.selectByExample(roleExample);
        return roles;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateUser(Hospusers hospusers, Integer roleid){
        int update = hospusersMapper.updateByPrimaryKeySelective(hospusers);
        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andRoleidEqualTo(roleid);
        criteria.andUseridEqualTo(hospusers.getUserid());
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
        if (userRoles.size() > 0) {return update;}
        else {
            UserRole userRole = new UserRole();
            userRole.setUserid(hospusers.getUserid());
            userRole.setRoleid(roleid);
            int insert = userRoleMapper.insert(userRole);
            update = insert;
        }
        return update;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int shanchuYonghu(Integer userid){
        Hospusers hospusers = new Hospusers();
        hospusers.setUserid(userid);
        hospusers.setZt(0);
        int update = hospusersMapper.updateByPrimaryKeySelective(hospusers);
        return update;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int multiDel(List<Integer> userids){
        int del = hospusersMapper.multiDel(userids);
        return del;
    }

    /**
     * 新增用户
     * @param hospusers
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addUser(Hospusers hospusers, Integer roleid) {
        int i = hospusersMapper.insertSelective(hospusers);
        UserRole userRole = new UserRole();
        userRole.setRoleid(roleid);
        userRole.setUserid(hospusers.getUserid());
        int insert = userRoleMapper.insert(userRole);
        if ( i >= 1 && insert >= 1) return 1;
        return 0;
    }
}
